package com.lm.scau.dyb.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.lm.scau.dyb.R;
import com.lm.scau.dyb.activity.base.BaseActivity;
import com.lm.scau.dyb.activity.base.RoleActivity;
import com.lm.scau.dyb.adapter.LMGridViewAdapter;
import com.lm.scau.dyb.bean.ADInfo;
import com.lm.scau.dyb.cycleviewpager.CycleViewPager;
import com.lm.scau.dyb.cycleviewpager.CycleViewPager.ImageCycleViewListener;
import com.lm.scau.dyb.db.Dalx.MovieTypeDalx;
import com.lm.scau.dyb.db.Dalx.PhotoWallDalx;
import com.lm.scau.dyb.inter.BasicUIEvent;
import com.lm.scau.dyb.utils.ImageLoaderUtil;
import com.lm.scau.dyb.view.LMGridView;

import java.util.ArrayList;
import java.util.List;

/**
 * Function:
 * auth：liming on 2016/8/5 11:02
 * mail：liming@wxchina.com
 */
public class HomeFragment extends Fragment implements BasicUIEvent {
    private final static int MSG_IO_PHOTO_WALLS_URL = 10;
    private final static int MSG_IO_CATEGORY_URL = 10;
    private BaseActivity context;
    private Handler localHandler;

    private List<ADInfo> infos;
    private List<String> photoWallUrls;
    private List<String> categotyUrls;
    private LMGridView mGridView;

    private ImageCycleViewListener mAdCycleViewListener = new ImageCycleViewListener() {

        @Override
        public void onImageClick(ADInfo info, int position, View imageView) {

        }

    };

    public HomeFragment() {
        PhotoWallDalx pDalx = new PhotoWallDalx((BaseActivity) getActivity());
        photoWallUrls = pDalx.getUrls();
        MovieTypeDalx mDalx = new MovieTypeDalx((BaseActivity) getActivity());
        categotyUrls = mDalx.getUrls();
        infos = new ArrayList<ADInfo>();
        initHandler();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (infos.size() == 0) {
            initUrls();
        }
    }

    private void initUrls() {
        if (null != photoWallUrls && photoWallUrls.size() > 0) {
            for (int i = 0; i < photoWallUrls.size(); i++) {
                ADInfo info = new ADInfo();
                info.setUrl(photoWallUrls.get(i));
                info.setContent("图片-->" + i);
                infos.add(info);
            }
        }
    }

    private void initHandler() {
        localHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {

            }
        };
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        mGridView = (LMGridView) view.findViewById(R.id.gridview);
        FragmentManager fm = getChildFragmentManager();
        CycleViewPager cycleViewPager = (CycleViewPager) fm.findFragmentById(R.id.fragment_cycle_viewpager_content);
        // 设置循环，在调用setData方法前调用
        cycleViewPager.setCycle(true);
        // 在加载数据前设置是否循环
        cycleViewPager.setData(infos, mAdCycleViewListener);
        //设置轮播
        cycleViewPager.setWheel(true);
        // 设置轮播时间，默认5000ms
        cycleViewPager.setTime(5000);
        LMGridViewAdapter mGridAdapter = new LMGridViewAdapter((BaseActivity) getActivity(), categotyUrls, null);
        mGridView.setAdapter(mGridAdapter);
        initAction();
        return view;
    }

    private void initAction() {
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String type = categotyUrls.get(position).split(",")[1];
                Bundle bundle = new Bundle();
                bundle.putString("type",type);
                ((BaseActivity)getActivity()).startActivity(RoleActivity.class,bundle);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
    }


    @Override
    public void execute(int msg, Object obj) {
        switch (msg) {
            case MSG_IO_PHOTO_WALLS_URL:
                break;
            default:
                break;
        }
    }


    private void setHandlerMsg(int what, Object obj) {
        Message message = Message.obtain(localHandler, what, obj);
        message.sendToTarget();
    }
}
